package com.rocksdata.mc.ui.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.ctz.commonframe.ui.TitleBarActivity;
import com.rocksdata.mc.BuildConfig;
import com.rocksdata.mc.business.BusinessInterface;
import com.rocksdata.mc.business.BusinessManager;
import com.rocksdata.mc.R;
import com.rocksdata.mc.business.UserInfoManager;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

/**
 * Created by 陈堂正 on 2017/3/17.
 */
@ContentView(R.layout.activity_login)
public class LoginActivity extends TitleBarActivity{
    @ViewInject(R.id.et_account)
    private EditText etAccount;
    @ViewInject(R.id.et_password)
    private EditText etPassword;
    @ViewInject(R.id.tv_visitor_login)
    private TextView tvVisitorLogin;
    @ViewInject(R.id.iv_logo)
    private ImageView ivLogo;

    private String account, password;
    private boolean showCancel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        showCancel = getIntent().getBooleanExtra("showCancel", false);
        if(showCancel){
            setTitleMiddleText("登录");
            tvVisitorLogin.setVisibility(View.GONE);
        }else{
            setBackgroundColor(Color.WHITE);
            setTitleEnable(false);
        }
        ivLogo.setImageResource(R.mipmap.ic_launcher);
        String account = UserInfoManager.getCurrentAccount();
        if (!TextUtils.isEmpty(account)) {
            etAccount.setText(account);
        }
    }

    private void gotoMainActivity(boolean isVisitor){
        UserInfoManager.saveIsVisitor(isVisitor);
        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
        if(showCancel)
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
        startActivity(intent);
        finish();
    }

    private void gotoModifyPasswordActivity () {
        Intent intent = new Intent(LoginActivity.this, ModifyPasswordActivity.class);
        startActivity(intent);
        finish();
    }

    private boolean checkInput(){
        account = etAccount.getText().toString().trim();
        password = etPassword.getText().toString().trim();
        if(TextUtils.isEmpty(account)){
            showToast("请输入账号");
            return false;
        }else if(TextUtils.isEmpty(password)){
            showToast("请输入密码");
            return false;
        }
        return true;
    }

    private void login(){
        showLoadingDialog();
        BusinessManager.getAccountManager().login(LoginActivity.this, account, password, new BusinessInterface.OnBusinessResultListener() {
            @Override
            public void onBusinessResult(boolean isSuccess, Object object, String message) {
                if(isSuccess){
                    if (UserInfoManager.getModifyPasswordFlag())
                        gotoMainActivity(false);
                    else
                        gotoModifyPasswordActivity();
                }else{
                    showToast("登录失败，请检查账号、密码");
                }
                dismissLoadingDialog();
            }
        });
    }

    @Event(value = {R.id.iv_account_clear, R.id.iv_password_clear, R.id.tv_user_login, R.id.tv_visitor_login})
    private void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_account_clear:
                etAccount.setText("");
                break;
            case R.id.iv_password_clear:
                etPassword.setText("");
                break;
            case R.id.tv_user_login:
                if(checkInput()){
                    login();
                }
                break;
            case R.id.tv_visitor_login:
                gotoMainActivity(true);
                break;
        }
    }
}
